Page({
  data: {
    favoriteList: [
      { id: 1, word: 'aberration', pronunciation: '/ˌæbəˈreɪʃn/', definition: '偏差，脱离常轨', example: 'The decision was an aberration from their usual practice.', isFavorite: true },
      { id: 2, word: 'abjure', pronunciation: '/əbˈdʒʊə(r)/', definition: '发誓放弃，宣誓弃绝', example: 'He abjured his allegiance to the king.', isFavorite: true },
      { id: 3, word: 'ablution', pronunciation: '/əˈbluːʃn/', definition: '沐浴，净身', example: 'The priest performed his ablutions before the ceremony.', isFavorite: true },
      { id: 4, word: 'abnegation', pronunciation: '/ˌæbnɪˈɡeɪʃn/', definition: '放弃，克制', example: 'Her abnegation of personal comfort for the sake of others was admirable.', isFavorite: true },
      { id: 5, word: 'aboriginal', pronunciation: '/ˌæbəˈrɪdʒənl/', definition: '土著的，原始的', example: 'The aboriginal people have lived in this area for thousands of years.', isFavorite: true }
    ],
    currentPage: 1,
    pageSize: 10,
    totalCount: 5
  },

  // 查看单词详情
  viewWordDetail(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/wordDetail/wordDetail?id=${id}&from=favorite`
    })
  },

  // 取消收藏
  unFavorite(e) {
    const { id } = e.currentTarget.dataset
    const { favoriteList } = this.data
    const newList = favoriteList.map(item => {
      if (item.id === id) {
        return { ...item, isFavorite: false }
      }
      return item
    }).filter(item => item.isFavorite)

    this.setData({
      favoriteList: newList,
      totalCount: newList.length
    })

    wx.showToast({
      title: '已取消收藏',
      icon: 'success'
    })
  },

  // 分页变化
  onPageChange(e) {
    this.setData({
      currentPage: e.detail.current + 1
    })
  },

  onLoad: function () {
    // 模拟从服务器加载收藏数据
    this.loadFavoriteWords()
  },

  // 加载收藏数据
  loadFavoriteWords() {
    // 这里是模拟数据，实际开发中应该从服务器获取
    const { favoriteList } = this.data
    this.setData({
      favoriteList,
      totalCount: favoriteList.length
    })
  }
})